package org.zhou.demo1;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class LogDbOperateImpl implements LogDbOperateApi {
    private Connection conn = null;
    private String url = "jdbc:h2:./03-adapter/adapter-log";
    private String table = "log";

    public LogDbOperateImpl(String url, String table) {
        if (url != null) {
            this.url = url;
        }

        if (table != null) {
            this.table = table;
        }

        try {
            Class.forName("org.h2.Driver");
            this.conn = DriverManager.getConnection(this.url);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

        if (this.conn == null) {
            throw new RuntimeException("数据库连接获取失败");
        }

        Statement stmt = null;
        try {
            stmt = this.conn.createStatement();
            String sql = "create table if not exists " + this.table + " (" +
                    "log_id varchar(10)," +
                    "operate_user varchar(100)," +
                    "operate_time varchar(20)," +
                    "log_content varchar(200)" +
                    ")";
            stmt.execute(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void createLog(LogModel logModel) {
        PreparedStatement pstmt = null;
        try {
            String sql = "insert into " + this.table + " values (?, ?, ?, ?)";
            pstmt = this.conn.prepareStatement(sql);
            pstmt.setString(1, logModel.getLogId());
            pstmt.setString(2, logModel.getOperateUser());
            pstmt.setString(3, logModel.getOperateTime());
            pstmt.setString(4, logModel.getLogContent());
            pstmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void updateLog(LogModel logModel) {
        PreparedStatement pstmt = null;
        try {
            String sql = "update " + this.table + " set operate_user=?, operate_time=?, log_content=? where log_id=?";
            pstmt = this.conn.prepareStatement(sql);
            pstmt.setString(1, logModel.getOperateUser());
            pstmt.setString(2, logModel.getOperateTime());
            pstmt.setString(3, logModel.getLogContent());
            pstmt.setString(4, logModel.getLogId());
            pstmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void removeLog(LogModel logModel) {
        PreparedStatement pstmt = null;
        try {
            String sql = "delete from " + this.table + " where log_id=?";
            pstmt = this.conn.prepareStatement(sql);
            pstmt.setString(1, logModel.getLogId());
            pstmt.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(pstmt != null) {
                try {
                    pstmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public List<LogModel> getAllLog() {
        List<LogModel> logModels = new ArrayList<>();

        Statement stmt = null;
        try {
            stmt = this.conn.createStatement();
            String sql = "select * from " + this.table;
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()) {
                String logId = rs.getString("log_id");
                String operateUser = rs.getString("operate_user");
                String operateTime = rs.getString("operate_time");
                String logContent = rs.getString("log_content");
                logModels.add(new LogModel(logId, operateUser, operateTime, logContent));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return logModels;
    }
}
